package com.spirngboot.middleware.middleware_rabbitmq.server.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class FanoutOrderRabbitMQConfig {
    private final String exchangeName = "fanout-order-exchange";
    private final String routingKey = "";

    //创建队列
    @Bean
    public Queue emailQueue() {
        // durable:是否持久化,默认是false,持久化队列：会被存储在磁盘上，当消息代理重启时仍然存在，暂存队列：当前连接有效
        // exclusive:默认也是false，只能被当前创建的连接使用，而且当连接关闭后队列即被删除。此参考优先级高于durable
        // autoDelete:是否自动删除，当没有生产者或者消费者使用此队列，该队列会自动删除。
        //   return new Queue("TestDirectQueue",true,true,false);
        //一般设置一下队列的持久化就好,其余两个就是默认false
        return new Queue("email.fanout.queue", true);
    }

    @Bean
    public Queue wechatQueue() {
        return new Queue("wechat.fanout.queue", true);
    }

    @Bean
    public Queue shortMessageQueue() {
        return new Queue("short_message.fanout.queue", true);
    }

    //创建交换机,可以看到,这里的交换机类已经是指明要创建的交换机类型
    @Bean
    public FanoutExchange fanoutOrderExchange() {
        //交换机名,是否持久化,是否自动删除
        return new FanoutExchange(exchangeName, true, false);
    }

    //绑定队列
    @Bean
    public Binding fanoutEmailQueueBinding() {
        return BindingBuilder.bind(emailQueue()).to(fanoutOrderExchange());
    }

    @Bean
    public Binding fanoutWechatFanoutQueueBinding() {
        return BindingBuilder.bind(wechatQueue()).to(fanoutOrderExchange());
    }

    @Bean
    public Binding fanoutShortMessageBinding() {
        return BindingBuilder.bind(shortMessageQueue()).to(fanoutOrderExchange());
    }
}
